package com.example.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.student.config.PageParam;
import com.example.student.entity.User;
import com.example.student.mapper.UserMapper;
import com.example.student.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 根据id获取单条
     * @param id
     * @author lx
     * @date 2022/2/20 11:26
     * @return
     */
    @Override
    public User getOneById (String id) {
        return userMapper.selectById(id);
    }

    /**
     * 根据手机号获取单条
     * @param account
     * @author lx
     * @date 2022/2/20 11:26
     * @return
     */
    @Override
    public User getOne (String account) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("account",account));
    }

    /**
     * 新增
     * @param user
     * @author lx
     * @date 2022/2/20 11:26
     * @return
     */
    @Override
    public boolean add (User user) {
        return userMapper.insert(user)>0;
    }

    /**
     * 修改
     * @param user
     * @author lx
     * @date 2022/2/20 11:26
     * @return
     */
    @Override
    public boolean update (User user) {
        return userMapper.updateById(user)>0;
    }

    /**
     * 删除
     * @param id
     * @author lx
     * @date 2022/2/20 11:27
     * @return
     */
    @Override
    public boolean delete(String id){
        return userMapper.deleteById(id)>0;
    }

    /**
     * 密码重置
     * @param ids
     * @author lx
     * @date 2021/1/27 16:54
     * @return
     */
    @Override
    public boolean restPassword(String[]  ids){
        for(String id:ids){
            User user = userMapper.selectById(id);
            user.setPassword("123456");
            userMapper.updateById(user);
        }
        return true;
    }

    /**
     * 分页查询
     * @param page
     * @author lx
     * @date 2022/2/20 23:20
     * @return
     */
    @Override
    public IPage page(PageParam page){
        return userMapper.page(new Page(page.getPage(),page.getSize()),page);
    }

}
